package com.daqsoft.service.system;

import com.daqsoft.dao.ModuleDao;
import com.daqsoft.data.Module;
import com.daqsoft.dto.IViewCascader;
import com.daqsoft.dto.ExtTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 周麟东 on 10:51
 */
@Service
public class ModuleService {
    @Autowired
    private ModuleDao moduleDao;

    public Module saveModule(Module module , int pid) {
        Module father = moduleDao.findOne(pid);
        module.setpModule(father);
        module = moduleDao.save(module);
        return module;
    }
    public List<Module> getAll() {
        return moduleDao.findAll();
    }

    /**
     * 获取树形结构
     * @return 树形结构
     */
    public List<ExtTreeNode> getAllByTree(){
        List<Module> modules = this.getPModules();
        return module2ExtTree(modules);
    }

    public List<ExtTreeNode> module2ExtTree(List<Module> modules){
        List<ExtTreeNode> extTreeNodes = new ArrayList<>();
        for (Module module : modules) {
            if (module.getId() == 0){
                continue;
            }
            ExtTreeNode father = new ExtTreeNode(module);
            if (module.getpModule() != null){
                father.setPid(module.getpModule().getId());
            }
            if (!module.isLeaf() && module.getId()!= 0){
                father.setChildren(module2ExtTree(getByPid(father.getId())));
            }
            father.setExpanded(true);
            extTreeNodes.add(father);
        }
        return extTreeNodes;
    }

    /**
     * 获取级联结构
     * @return 树形结构
     */
    public List<IViewCascader> getAllByCascader() {
        List<Module> modules = this.getPModules();
        List<IViewCascader> cascaders = new ArrayList<>();
        for (Module module : modules) {
            IViewCascader father = new IViewCascader();
            father.setLabel(module.getModuleName());
            father.setValue(module.getId()+"");
            List<IViewCascader> children = new ArrayList<>();
            for (Module m : module.getChildren()) {
                IViewCascader child = new IViewCascader();
                child.setLabel(m.getModuleName());
                child.setValue(m.getId()+"");
                children.add(child);
            }
            father.setChildren(children);
            cascaders.add(father);
        }
        return cascaders;
    }

    public void remove(int id) {
        moduleDao.delete(id);
    }

    public Module get(int id) {
        return moduleDao.findOne(id);
    }

    public List<Module> getByPid(int pid){
        Module module = moduleDao.findOne(pid);
        return moduleDao.getModuleByPModuleOrderByLevel(module);
    }

//    public List<Map<String, Object>> module2Map(List<Module> modules) {
//        List<Map<String, Object>> res = new ArrayList<>();
//        for (Module module : modules) {
//            res.add(module.toMap());
//        }
//        return res;
//    }

    /**
     * 获取所有父级菜单
     *
     * @return 所有父级菜单
     */
    public List<Module> getPModules() {
        return moduleDao.getModuleByLeafOrderByLevel(false);
    }


    /**
     * 通过用户获取当前用户权限下所有的模块
     * @return 模块
     */
//    public List<Module> getModuleByPermission(List<Permission> permissions){
//        List<Module> modules = getPModules();
//        if (dev){
//            return modules;
//        }
//        modules = modules.stream().filter(module -> {
//            for (Permission permission : permissions) {
//                if (module.getId() == permission.getModuleId()){
//                    return true;
//                }
//            }
//            return false;
//        }).collect(Collectors.toList());
//        return modules;
//    }
}
